package com.bgq365.study.springboot2.boot02web.config;

import com.bgq365.study.springboot2.boot02web.interceptor.LoginInterceptor;
import com.bgq365.study.springboot2.boot02web.interceptor.RedisCountInterceptor;
import com.bgq365.study.springboot2.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    /*
    矩阵变量生效的方法二
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }*/

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new HttpMessageConverter<User>() {
            @Override
            public boolean canRead(Class<?> clazz, MediaType mediaType) {
                return false;
            }

            @Override
            public boolean canWrite(Class<?> clazz, MediaType mediaType) {
                return clazz.isAssignableFrom(User.class);
            }

            @Override
            public List<MediaType> getSupportedMediaTypes() {
                return MediaType.parseMediaTypes("application/x-jason");
            }

            @Override
            public User read(Class<? extends User> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
                return null;
            }

            @Override
            public void write(User user, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
                String data = user.toString();
                outputMessage.getBody().write(data.getBytes());
            }
        });
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.mediaType("x-jason",MediaType.parseMediaType("application/x-jason"));
    }

    /**
     * Filter、Interceptor 几乎有相同的功能？
     * 1、Filter是Servlet定义的原生组件。好处是脱离了Spring应用也可以使用
     * 2、Interceptor是Spring定义的接口。可以使用Spring的自动装配等功能
     */
//    @Autowired
//    RedisCountInterceptor redisCountInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(Arrays.asList("/","/login","/resources/**"));

//        registry.addInterceptor(redisCountInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(Arrays.asList("/","/login","/resources/**"));
    }
}
